package com.easyfile.easyfilemaster.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easyfile.easyfilemaster.entity.FileInfo;
import com.easyfile.easyfilemaster.entity.FolderDetail;
import com.easyfile.easyfilemaster.entity.FolderInfo;
import com.easyfile.easyfilemaster.entity.annotations.RateLimit;
import com.easyfile.easyfilemaster.entity.base.Result;
import com.easyfile.easyfilemaster.entity.base.ResultFactory;
import com.easyfile.easyfilemaster.entity.dto.*;
import com.easyfile.easyfilemaster.entity.vo.MultipartUploadVo;
import com.easyfile.easyfilemaster.service.FileService;
import com.easyfile.easyfilemaster.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpRequest;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 文件业务接口
 *
 * @author chenpeman
 * @date 2022年05月05日 21:54
 */
@RestController
@RequestMapping("/file/")
@Validated
@Slf4j
public class FileController {

    @Resource
    private FileService fileService;

    /**
     * 获取用户的根目录信息
     *
     * @param getDefaultFolderDto 请求参数
     * @param token               token
     * @return 根目录信息
     */
    @PostMapping("getDefaultFolder")
    @RateLimit(limit = 10)
    public Result<FolderInfo> getDefaultFolder(
            @Validated GetDefaultFolderDto getDefaultFolderDto,
            @RequestHeader("X-Token") String token){
        Long userId = JwtUtil.getUserIdByToken(token);
        getDefaultFolderDto.setUserId(userId);
        return fileService.getDefaultFolder(getDefaultFolderDto);
    }

    /**
     * 获取某一目录下的所有文件夹
     *
     * @param getChildFoldersDto 请求参数
     * @param token              token
     * @return 文件夹列表
     */
    @PostMapping("getChildFolders")
    @RateLimit(limit = 10)
    public Result<IPage<FolderInfo>> getChildFolders(
            @Validated GetChildFoldersDto getChildFoldersDto,
            @RequestHeader("X-Token") String token){
        Long userId = JwtUtil.getUserIdByToken(token);
        getChildFoldersDto.setUserId(userId);
        return fileService.getChildFolders(getChildFoldersDto);
    }

    /**
     * 获取某一目录下所有的文件
     *
     * @param getChildFilesDto 请求参数
     * @param token            token
     * @return 文件列表
     */
    @PostMapping("getChildFiles")
    @RateLimit(limit = 10)
    public Result<IPage<FileInfo>> getChildFiles(
            @Validated GetChildFilesDto getChildFilesDto,
            @RequestHeader("X-Token") String token){
        Long userId = JwtUtil.getUserIdByToken(token);
        getChildFilesDto.setUserId(userId);
        return fileService.getChildFiles(getChildFilesDto);
    }

    /**
     * 根据关键字搜索文件
     *
     * @param dto   the dto
     * @param token the token
     * @return the result
     */
    @PostMapping("searchFiles")
    @RateLimit(limit = 10)
    public Result<IPage<FileInfo>> searchFiles(
            @Validated SearchFilesDto dto,
            @RequestHeader("X-Token") String token){
        Long userId = JwtUtil.getUserIdByToken(token);
        dto.setUserId(userId);
        return fileService.searchFiles(dto);
    }

    /**
     * 根据关键字搜索文件夹
     *
     * @param dto   the dto
     * @param token the token
     * @return the result
     */
    @PostMapping("searchFolders")
    @RateLimit(limit = 10)
    public Result<IPage<FolderInfo>> searchFolders(
            @Validated SearchFoldersDto dto,
            @RequestHeader("X-Token") String token){
        Long userId = JwtUtil.getUserIdByToken(token);
        dto.setUserId(userId);
        return fileService.searchFolders(dto);
    }

    /**
     * 通过类型id检索文件
     *
     * @param dto   the dto
     * @param token the token
     * @return the files by type id
     */
    @PostMapping("getFilesByTypeId")
    @RateLimit(limit = 10)
    public Result<IPage<FileInfo>> getFilesByTypeId(
            @Validated GetTypeFilesDto dto,
            @RequestHeader("X-Token") String token){
        Long userId = JwtUtil.getUserIdByToken(token);
        dto.setUserId(userId);
        return fileService.getFilesByTypeId(dto);
    }

    /**
     * 根据关键字搜索某一类型的文件
     *
     * @param dto   the dto
     * @param token the token
     * @return the result
     */
    @PostMapping("searchTypeFiles")
    @RateLimit(limit = 10)
    public Result<IPage<FileInfo>> searchTypeFiles(
            SearchTypeFilesDto dto,
            @RequestHeader("X-Token") String token){
        Long userId = JwtUtil.getUserIdByToken(token);
        dto.setUserId(userId);
        return fileService.searchTypeFiles(dto);
    }


    /**
     * 下载多个文件
     *
     * @param downloadFilesDto 氢气汆熟
     * @param token            token
     * @return 下载的临时url列表
     */
    @PostMapping("downloadFiles")
    @RateLimit(limit = 10)
    public Result<List<String>> downloadFiles(
            @Validated DownloadFilesDto downloadFilesDto,
            @RequestHeader("X-Token") String token){
        Long userId = JwtUtil.getUserIdByToken(token);
        downloadFilesDto.setUserId(userId);
        return fileService.downloadFiles(downloadFilesDto);
    }


    /**
     * 在某一目录下插入文件夹
     *
     * @param insertFolderDto the insert folder dto
     * @param token           the token
     * @return the result
     */
    @PostMapping("insertFolder")
    @RateLimit(limit = 10)
    public Result<Void> insertFolder(
            @Validated InsertFolderDto insertFolderDto,
            @RequestHeader("X-Token") String token){
        Long userId = JwtUtil.getUserIdByToken(token);
        insertFolderDto.setUserId(userId);
        return fileService.insertFolder(insertFolderDto);
    }

    /**
     * 重命名文件夹
     *
     * @param renameFolderDto to rename folder dto
     * @param token           the token
     * @return the result
     */
    @PostMapping("renameFolder")
    @RateLimit(limit = 10)
    public Result<Void> renameFolder(
            @Validated RenameFolderDto renameFolderDto,
            @RequestHeader("X-Token") String token){
        Long userId = JwtUtil.getUserIdByToken(token);
        renameFolderDto.setUserId(userId);
        return fileService.renameFolder(renameFolderDto);
    }

    /**
     * 重命名文件
     *
     * @param renameFileDto the rename file dto
     * @param token         the token
     * @return the result
     */
    @PostMapping("renameFile")
    @RateLimit(limit = 10)
    public Result<Void> renameFile(
            @Validated RenameFileDto renameFileDto,
            @RequestHeader("X-Token") String token){
        Long userId = JwtUtil.getUserIdByToken(token);
        renameFileDto.setUserId(userId);
        return fileService.renameFile(renameFileDto);
    }


    /**
     * 批量删除文件
     *
     * @param dto   the dto
     * @param token the token
     * @return the result
     */
    @PostMapping("deleteFiles")
    @RateLimit(limit = 10)
    public Result<Void> deleteFiles(
            @Validated DeleteFilesDto dto,
            @RequestHeader("X-Token") String token){
        Long userId = JwtUtil.getUserIdByToken(token);
        dto.setUserId(userId);
        return fileService.deleteFiles(dto);
    }

    /**
     * 批量删除文件夹
     *
     * @param dto   the dto
     * @param token the token
     * @return the result
     */
    @PostMapping("deleteFolders")
    @RateLimit(limit = 10)
    public Result<Void> deleteFolders(
            @Validated DeleteFoldersDto dto,
            @RequestHeader("X-Token") String token){
        Long userId = JwtUtil.getUserIdByToken(token);
        dto.setUserId(userId);
        return fileService.deleteFolders(dto);
    }

    /**
     * 批量移动文件
     *
     * @param dto   the dto
     * @param token the token
     * @return the result
     */
    @PostMapping("moveFiles")
    @RateLimit(limit = 10)
    public Result<Void> moveFiles(
            @Validated MoveFilesDto dto,
            @RequestHeader("X-Token") String token){
        Long userId = JwtUtil.getUserIdByToken(token);
        dto.setUserId(userId);
        return fileService.moveFiles(dto);
    }

    /**
     * 批量复制文件
     *
     * @param dto   the dto
     * @param token the token
     * @return the result
     */
    @PostMapping("copyFiles")
    @RateLimit(limit = 10)
    public Result<Void> copyFiles(
            @Validated CopyFilesDto dto,
            @RequestHeader("X-Token") String token){
        Long userId = JwtUtil.getUserIdByToken(token);
        dto.setUserId(userId);
        return fileService.copyFiles(dto);
    }


    /**
     * 分片上传 获取分片的预签名上传地址
     *
     * @param dto   the dto
     * @param token the token
     * @return the presigned chunk urls
     */
    @PostMapping("getPresignedChunkUrls")
    public Result<MultipartUploadVo> getPresignedChunkUrls(
            @Validated GetPresignedChunkUrlsDto dto,
            @RequestHeader("X-Token") String token){
        Long userId = JwtUtil.getUserIdByToken(token);
        dto.setUserId(userId);
        return fileService.getPresignedChunkUrls(dto);
    }

    /**
     * 分片上传完成，请求合并分片
     *
     * @param dto   the dto
     * @param token the token
     * @return the result
     */
    @PostMapping("completeMultipartUpload")
    public Result<Void> completeMultipartUpload(
            @Validated CompleteMultipartUploadDto dto,
            @RequestHeader("X-Token") String token){
        Long userId = JwtUtil.getUserIdByToken(token);
        dto.setUserId(userId);
        return fileService.completeMultipartUpload(dto);
    }

    /**
     * 批量分享文件
     *
     * @param dto   the dto
     * @param token the token
     * @return the result
     */
    @PostMapping("shareFiles")
    @RateLimit(limit = 10)
    public Result<String> shareFiles(
            @Validated ShareFilesDto dto,
            @RequestHeader("X-Token") String token){
        Long userId = JwtUtil.getUserIdByToken(token);
        dto.setUserId(userId);
        return fileService.shareFiles(dto);
    }

    /**
     * 通过分享密钥保存分享的文件
     *
     * @param dto the dto
     * @return the result
     */
    @PostMapping("saveShareFiles")
    @RateLimit(limit = 10)
    public Result<Void> saveShareFiles(
            @Validated SaveShareFilesDto dto,
            @RequestHeader("X-Token") String token){
        Long userId = JwtUtil.getUserIdByToken(token);
        dto.setUserId(userId);
        return fileService.saveShareFiles(dto);
    }
}
